﻿<#
.SYNOPSIS 
    Assigns the PeopleSoft Application Server features.
.DESCRIPTION 
    Use this command to set the PeopleSoft Application Server advanced configuration parameters for a specific domain.
    This command configures which servers are maked to boot when the Application Server boots (PUBSUB, JOLT, etc)
    Register-PsftConfiguration *must* be executed after running Set-PsftFeature.
.NOTES 
    File Name  : Set-PsftFeature.ps1 
    Author     : Scott Kearney - scott@kearney.tk 
    Requires   : PowerShell V3 
.EXAMPLE 
    Set-PsftFeature -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -ServerType AppServer -Domain DOMAIN1 -JOLT $true
    Enables the JOLT server on DOMAIN1.
.EXAMPLE
    Set-PsftFeature -PS_HOME C:\psoft\PS_HOME `
                  -PS_CFG_HOME C:\psoft\PS_CFG_HOME `
                  -ServerType AppServer `
                  -Domain DOMAIN1 `
                  -PUBSUB $false `
                  -QUICKSRV $false `
                  -QUERYSRV $false `
                  -JOLT $true `
                  -JRAD $false `
                  -WSL $true `
                  -DBGSRV $false `
                  -RENSRV $false `
                  -MCF $false `
                  -PPM $false `
                  -ANALYTICSRV $false `
                  -DOMAIN_GW $false
    
    Sets each of the available features to the desired value.
.EXAMPLE 
    Get-PsftFeature -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -DOMAIN1 | 
        Set-PsftFeature -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -ServerType AppServer -Domain DOMAIN2
    Sets the features on DOMAIN2 to match DOMAIN1
.PARAMETER PS_HOME
   The full path to the PS_HOME for this domain.
.PARAMETER PS_CFG_HOME
    The full path to the PS_CFG_HOME for this domain.
.PARAMETER Domain
    The name of the domain on which to perform the action.
.PARAMETER PUBSUB
   Pub/Sub Servers - Set to $true to enable, $false to disable.
.PARAMETER QUICKSRV
   Quick Server - Set to $true to enable, $false to disable.
.PARAMETER QUERYSRV
   Query Server - Set to $true to enable, $false to disable.
.PARAMETER JOLT
   Jolt - Set to $true to enable, $false to disable.
.PARAMETER JRAD
   Jolt Relay - Set to $true to enable, $false to disable.
.PARAMETER WSL
   Workstation Listener - Set to $true to enable, $false to disable.
.PARAMETER DBGSRV
   PC Debugger - Set to $true to enable, $false to disable.
.PARAMETER RENSRV
   Event Notification - Set to $true to enable, $false to disable.
.PARAMETER MCF
   MCF Servers - Set to $true to enable, $false to disable.
.PARAMETER PPM
   Perf Collator - Set to $true to enable, $false to disable.
.PARAMETER ANALYTICSRV
   Analytic Servers - Set to $true to enable, $false to disable.
.PARAMETER DOMAIN_GW
   Domains Gateway - Set to $true to enable, $false to disable.
.PARAMETER APPENG
   App Engine - Set to $true to enable, $false to disable.
.PARAMETER MSTRSRV
   Master Scheduler - Set to $true to enable, $false to disable.
#> 
function Set-PsftFeature
{
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
    [OutputType([boolean])]
    Param
    (
        # The PS_HOME directory.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Path to the PS_HOME directory.",
                   Position=0)]
        [ValidateScript({Validate_PS_HOME $_})]
        [String]
        $PS_HOME,

        # The PS_CFG_HOME directory.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Path to the PS_CFG_HOME directory.",
                   Position=1)]
        [ValidateScript({Validate_PS_CFG_HOME $_})]
        [String]
        $PS_CFG_HOME,

        # The type of server on which to perform the action.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The type of server on which to perform the action.",
                   Position=2)]
        [ValidateSet("AppServer","PrcsScheduler")]
        $ServerType,

        # The name of the domain on which to perform the action.
        # Should be a directory under the PS_CFG_HOME
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The name of the domain on which to perform the action.",
                   Position=3)]
        [ValidateScript({Validate_Domain $PS_CFG_HOME $ServerType $_})]
        $Domain,

        # Pub/Sub Servers
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Pub/Sub Servers",
                   Position=3,
                   ParameterSetName = "AppServer")]
        [boolean]
        $PUBSUB,

        # Quick Server
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Quick Server",
                   Position=4,
                   ParameterSetName = "AppServer")]
        [boolean]
        $QUICKSRV,

        # Query Server
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Query Server",
                   Position=5,
                   ParameterSetName = "AppServer")]
        [boolean]
        $QUERYSRV,

        # Jolt
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Jolt",
                   Position=6,
                   ParameterSetName = "AppServer")]
        [boolean]
        $JOLT,

        # Jolt Relay
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Jolt Relay",
                   Position=7,
                   ParameterSetName = "AppServer")]
        [boolean]
        $JRAD,

        # Workstation Listener
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Workstation Listener",
                   Position=8,
                   ParameterSetName = "AppServer")]
        [boolean]
        $WSL,

        # PC Debugger
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "PC Debugger",
                   Position=9,
                   ParameterSetName = "AppServer")]
        [boolean]
        $DBGSRV,

        # Event Notification
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Event Notification",
                   Position=10,
                   ParameterSetName = "AppServer")]
        [boolean]
        $RENSRV,

        # MCF Servers
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "MCF Servers",
                   Position=11,
                   ParameterSetName = "AppServer")]
        [boolean]
        $MCF,

        # Perf Collator
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Perf Collator",
                   Position=12,
                   ParameterSetName = "AppServer")]
        [boolean]
        $PPM,

        # Analytic Servers
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Analytic Servers",
                   Position=13,
                   ParameterSetName = "AppServer")]
        [boolean]
        $ANALYTICSRV,

        # Domains Gateway
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Domains Gateway",
                   Position=14,
                   ParameterSetName = "AppServer")]
        [boolean]
        $DOMAIN_GW,

        # App Engine
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "App Engine",
                   Position=15,
                   ParameterSetName = "PrcsScheduler")]
        [boolean]
        $APPENG,

        # Master Scheduler
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Master Scheduler",
                   Position=16,
                   ParameterSetName = "PrcsScheduler")]
        [boolean]
        $MSTRSRV
    )

    Begin
    {
    }
    Process
    {
        # Get the list of features as they are already defined
        $features = Get-PsftFeature -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain
        
        # If any of the values was passed to this function, set that value in the $feature list
        if($PSBoundParameters.Keys -contains "PUBSUB"     ){$features.PUBSUB      = $PUBSUB     }
        if($PSBoundParameters.Keys -contains "QUICKSRV"   ){$features.QUICKSRV    = $QUICKSRV   }
        if($PSBoundParameters.Keys -contains "QUERYSRV"   ){$features.QUERYSRV    = $QUERYSRV   }
        if($PSBoundParameters.Keys -contains "JOLT"       ){$features.JOLT        = $JOLT       }
        if($PSBoundParameters.Keys -contains "JRAD"       ){$features.JRAD        = $JRAD       }
        if($PSBoundParameters.Keys -contains "WSL"        ){$features.WSL         = $WSL        }
        if($PSBoundParameters.Keys -contains "DBGSRV"     ){$features.DBGSRV      = $DBGSRV     }
        if($PSBoundParameters.Keys -contains "RENSRV"     ){$features.RENSRV      = $RENSRV     }
        if($PSBoundParameters.Keys -contains "MCF"        ){$features.MCF         = $MCF        }
        if($PSBoundParameters.Keys -contains "PPM"        ){$features.PPM         = $PPM        }
        if($PSBoundParameters.Keys -contains "ANALYTICSRV"){$features.ANALYTICSRV = $ANALYTICSRV}
        if($PSBoundParameters.Keys -contains "DOMAIN_GW"  ){$features.DOMAIN_GW   = $DOMAIN_GW  }
        if($PSBoundParameters.Keys -contains "APPENG"     ){$features.APPENG      = $APPENG     }
        if($PSBoundParameters.Keys -contains "MSTRSRV"    ){$features.MSTRSRV     = $MSTRSRV    }

        # Ensure that the user would like us to perform this action.
        if (($pscmdlet.ShouldProcess("$Domain", "Set-PsftFeature")))
        {
            # Invoke UBBGen to re-process the files and create the necessary UBB.
            $null = Invoke-UBBGen -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain -UBXSettingsObject $features
        }

        Write-Warning "Register-PsftConfiguration *must* be executed after running Set-PsftFeature."
    }
    End
    {
    }
}